package com.hedl.classroom.service.impl;

import com.alibaba.fastjson2.JSON;
import com.hedl.classroom.base.exception.OnlinecClassroomException;
import com.hedl.classroom.config.PayNotifyConfig;
import com.hedl.classroom.mapper.XcChooseCourseMapper;
import com.hedl.classroom.model.po.MqMessage;
import com.hedl.classroom.po.XcChooseCourse;
import com.hedl.classroom.po.XcCourseTables;
import com.hedl.classroom.service.MyCourseTablesService;
import com.hedl.classroom.service.ReceivePayNotifyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author Big Watermelon
 * @description 接受消息通知处理类
 * @date 2023/12/20/14:26
 */
@Slf4j
@Service
public class ReceivePayNotifyServiceImpl implements ReceivePayNotifyService {

    @Resource
    private XcChooseCourseMapper xcChooseCourseMapper;

    @Resource
    private MyCourseTablesService myCourseTablesService;

    @Resource
    ReceivePayNotifyServiceImpl receivePayNotifyServiceProxy;


    @RabbitListener(queues = PayNotifyConfig.PAYNOTIFY_QUEUE)
    @Override
    public void receive(Message message){

        try {
            //休息5秒
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //解析出消息
        byte[] body = message.getBody();
        String jsonString = new String(body);
        //转成对象
        MqMessage mqMessage = JSON.parseObject(jsonString, MqMessage.class);
        //解析消息内容
        //选课id
        String chooseCourseId = mqMessage.getBusinessKey1();
        //订单类型
        String orderType = mqMessage.getBusinessKey2();
        //学习中心服务只要购买课程类的支付订单的结果
        if (orderType.equals("60201")){
            //根据消息内容，更新选课记录，向我的课程插入记录
            boolean b = receivePayNotifyServiceProxy.saveChooseCourseSuccess(chooseCourseId);
            if (!b){
                OnlinecClassroomException.cast("保存选课记录失败！");
            }
        }
    }

    @Transactional
    @Override
    public boolean saveChooseCourseSuccess(String chooseCourseId) {

        //根据选课id查询课表
        XcChooseCourse xcChooseCourse = xcChooseCourseMapper.selectById(chooseCourseId);
        if (xcChooseCourse == null){
            log.debug("接收购买课程的信息，根据选课id从数据库找不到选课记录，选课id：{}",chooseCourseId);
            return false;
        }
        //选课状态
        String status = xcChooseCourse.getStatus();
        //只有当未支付时才更新为已支付
        if ("701002".equals(status)){
            //更新选课记录的状态为支付成功
            xcChooseCourse.setStatus("701001");
            int i = xcChooseCourseMapper.updateById(xcChooseCourse);
            if (i <= 0){
                log.debug("添加选课记录失败:{}",xcChooseCourse);
                OnlinecClassroomException.cast("添加选课记录失败!");
            }
            //向我的课程表插入记录
            XcCourseTables xcCourseTables = myCourseTablesService.addCourseTables(xcChooseCourse);
            return true;
        }
        return false;
    }
}
